﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ECGadmin.Common;

namespace ECGadmin.DAL
{
    public partial class ConfigDb
    {
        /// <summary>
        /// Lấy về tất cả bản ghi, phân trang
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="itemsPerPage"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public static List<ViewConfigPagingDto> GetAllPaging(int currentPage, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Config_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@CurrentPage", currentPage));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter totalPageItem = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                totalPageItem.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ViewConfigPagingDto>(ref command);

                totalPage = int.Parse(totalPageItem.Value.ToString() == "" ? "0" : totalPageItem.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Thêm bản ghi mới vào bàng Config
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int Add(ConfigDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Config_Add");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Key", model.Key));
                command.Parameters.Add(dao.CreateParameter("@Value", model.Value));
                command.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                command.Parameters.Add(dao.CreateParameter("@Type", model.Type));
                return (int)command.ExecuteScalar();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về bản ghi theo ConfigId
        /// </summary>
        /// <param name="configId"></param>
        /// <returns></returns>
        public static ConfigDto GetById(int configId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Config_GetById");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ConfigId", configId));
                return dao.GetSingleDTO<ConfigDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Cập nhật bản ghi
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int Update(ConfigDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Config_Update");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ConfigId", model.ConfigId));
                command.Parameters.Add(dao.CreateParameter("@Key", model.Key));
                command.Parameters.Add(dao.CreateParameter("@Value", model.Value));
                command.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                command.Parameters.Add(dao.CreateParameter("@Type", model.Type));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Xóa bản ghi theo ConfigId
        /// </summary>
        /// <param name="confingId"></param>
        /// <returns></returns>
        public static int Delete(int confingId)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Config_Delete");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@ConfigId", confingId));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
